﻿
Create PROCEDURE [dbo].[proc_Assets_Update_Selected] 
	(	
		@CompanyId int,
		@DepartmentId int,
		@UserName nvarchar(50),
		@EName nvarchar(50),
		@StateId int,	
		@DJUserName nvarchar(50),
		@DJDate datetime,	
		@checks VARCHAR(1000),
		@userid VARCHAR(30),
		@username_1 NVARCHAR(50)
	)
AS
BEGIN
	DECLARE @cSql NVARCHAR(4000)
	SET @cSql='
	Update a Set 
		a.DepartmentId = ' + Convert(varchar(10),@DepartmentId) + ',
		a.UserName = ''' + @UserName + ''',
		a.EName = ''' + @EName + ''',
		a.StateId = ' + Convert(varchar(10),@StateId) + ',
		a.DJUserName = ''' + @DJUserName + ''',
		a.DJDate = ''' + Convert(varchar(30),@DJDate,21) + ''',
		a.LyDate = getdate(),
		a.oldEName = b.ename,
		a.oldUserName = b.username,
		a.CompanyId = ' + Convert(varchar(10),@CompanyId) + '
	    from assets a,assets b
		Where b.id=a.id and a.id in (' + @checks + ');
		    INSERT INTO [zcjl]([zcbh] ,[memo] ,[lrrq] ,[userid] ,[username])
	SELECT anumber,	
    (CASE
		WHEN oldename='''' THEN ''该闲置资产，现领用人''+'''+@ename+''' + ''(''+ '''+@username+''' + '')''
		WHEN oldename<>'''' THEN ''该资产原使用人''+oldename+''(''+oldusername+'')，现领用人''+'''+@ename+''' + ''(''+ '''+@username+''' + '')''
     end),
		getdate(),''' + @userid + ''',''' + @username_1 + ''' FROM Assets WHERE id IN (' + @checks + ');
    '
    --SELECT @cSql;  
	EXEC(@cSql);
END